/*
 Copyright (c) 2013 Katsuma Tanaka
 
 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
 
 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
 
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#import "QBImagePickerGroupCell.h"

@implementation QBImagePickerGroupCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    if (self) {
        /* Initialization */
        // Title
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
        titleLabel.font = [UIFont boldSystemFontOfSize:17];
        titleLabel.textColor = [UIColor blackColor];
        titleLabel.highlightedTextColor = [UIColor whiteColor];
        titleLabel.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth;
        
        titleLabel.backgroundColor=[UIColor clearColor];
        [self.contentView addSubview:titleLabel];
        
        self.titleLabel = titleLabel;
        
        // Count
        UILabel *countLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
        countLabel.font = [UIFont systemFontOfSize:17];
        countLabel.textColor = [UIColor colorWithWhite:0.498 alpha:1.0];
        countLabel.highlightedTextColor = [UIColor whiteColor];
        countLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
        
        countLabel.backgroundColor=[UIColor clearColor];
        [self.contentView addSubview:countLabel];
        self.countLabel = countLabel;
    }
    
    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];
    
    self.titleLabel.highlighted = selected;
    self.countLabel.highlighted = selected;
}

/*
 
 _userNickname=[[UILabel alloc]initWithFrame:CGRectZero];
 _messageConent=[[UILabel alloc]initWithFrame:CGRectZero];
 _timeLable=[[UILabel alloc]initWithFrame:CGRectZero];
 _cellBkg=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"MessageListCell_bg"]];
 _bageView=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"tabbar_badge"]];
 _bageNumber=[[UILabel alloc]initWithFrame:CGRectZero];
 
 [_userHead.layer setCornerRadius:8.0f];
 [_userHead.layer setMasksToBounds:YES];
 
 [_userNickname setFont:[UIFont boldSystemFontOfSize:15]];
 [_userNickname setTextColor:SpecialGrayColor];
 [_userNickname setBackgroundColor:[UIColor clearColor]];
 [_messageConent setFont:[UIFont systemFontOfSize:12]];
 [_messageConent setTextColor:ContentLightGrayColor];
 [_messageConent setBackgroundColor:[UIColor clearColor]];
 
 [_timeLable setTextColor:[UIColor lightGrayColor]];
 [_timeLable setFont:[UIFont systemFontOfSize:15]];
 [_timeLable setTextAlignment:NSTextAlignmentRight];
 [_timeLable setBackgroundColor:[UIColor clearColor]];
 
 [_bageNumber setBackgroundColor:[UIColor clearColor]];
 [_bageNumber setTextAlignment:NSTextAlignmentCenter];
 [_bageNumber setTextColor:[UIColor whiteColor]];
 [_bageNumber setFont:[UIFont boldSystemFontOfSize:10]];
 
 [self setBackgroundView:_cellBkg];
 
 [self.contentView addSubview:_userHead];
 
 [self.contentView addSubview:_userNickname];
 [self.contentView addSubview:_messageConent];
 [self.contentView addSubview:_timeLable];
 [self.contentView addSubview:_bageView];
 [_bageView addSubview:_bageNumber];
 
 self.backgroundColor = [UIUtil colorWithHexString:@"#f9f5eb" withAlpha:1.0];
 
 self.selectedBackgroundView = [[UIView alloc] initWithFrame:self.frame];
 self.selectedBackgroundView.backgroundColor = [UIUtil colorWithHexString:@"#d4edf4" withAlpha:1.0];
 }
 return self;

 
 */

- (void)layoutSubviews
{
    [super layoutSubviews];
    //self.contentView
    CGRect rr = self.imageView.frame;
    rr.origin.x=10;
    CGFloat height = self.contentView.bounds.size.height;
    rr.origin.y = 2.5;
    self.imageView.frame =rr;

    CGFloat imageViewSize = height - 5;
    CGFloat width = self.contentView.bounds.size.width - 20;
    
    [self.contentView addSubview:self.imageView];
    CGSize titleTextSize = [self.titleLabel.text sizeWithFont:self.titleLabel.font forWidth:width lineBreakMode:NSLineBreakByTruncatingTail];
    CGSize countTextSize = [self.countLabel.text sizeWithFont:self.countLabel.font forWidth:width lineBreakMode:NSLineBreakByTruncatingTail];
    
    CGRect titleLabelFrame;
    CGRect countLabelFrame;
    
    if ((titleTextSize.width + countTextSize.width + 10) > width) {
        titleLabelFrame = CGRectMake(imageViewSize + 10+10, 0, width - countTextSize.width - 20, imageViewSize);
        countLabelFrame = CGRectMake(titleLabelFrame.origin.x + titleLabelFrame.size.width + 10, 0, countTextSize.width, imageViewSize);
    } else {
        titleLabelFrame = CGRectMake(imageViewSize + 10+10, 0, titleTextSize.width, imageViewSize);
        countLabelFrame = CGRectMake(titleLabelFrame.origin.x + titleLabelFrame.size.width + 10, 0, countTextSize.width, imageViewSize);
    }
    
    self.titleLabel.frame = titleLabelFrame;
    self.countLabel.frame = countLabelFrame;
}

@end
